/**
 * Create by richliu1023
 * @date 16/5/19
 * @email richliu1023@gmail.com
 * @github https://github.com/RichLiu1023
 * @description a simple explanation
 */
class WorldCanvas {
	private _canvas3d:egret3d.Egret3DCanvas;
	private _view3d:egret3d.View3D;
	private _view3dChildren:egret3d.Object3D[] = [];

	public constructor() {
		this.initCanvas();
		this.initHtmlCanvas();
	}

	private initCanvas():void {
		this._canvas3d = new egret3d.Egret3DCanvas();
		this._canvas3d.width = GameConfig.STAGE_W;
		this._canvas3d.height = GameConfig.STAGE_H;
		this._canvas3d.x = this._canvas3d.y = 0;
		//this._canvas3d.fps = 30;
		this._view3d = new egret3d.View3D( 0, 0, GameConfig.STAGE_W, GameConfig.STAGE_H );
		this._view3d.backColor = 0x000000;
		this._canvas3d.addView3D( this._view3d );
		this.onResize();
		RM.RMCanvas2DView.eventDispatcher.addEventListener( egret.Event.RESIZE, this.onResize, this );
	}

	public get canvas3d():egret3d.Egret3DCanvas {
		return this._canvas3d;
	}

	public get view3d():egret3d.View3D {
		return this._view3d;
	}

	public get camera3D():egret3d.Camera3D {
		return this._view3d.camera3D;
	}


	public addChild3D( $child3d:egret3d.Object3D ):void {
		this._view3d.addChild3D( $child3d );
		this._view3dChildren.push( $child3d );
	}

	public removeChild3D( $child3d:egret3d.Object3D ):void {
		var index:number = this._view3dChildren.indexOf( $child3d );
		if ( index == -1 )return console.log( 'remove child no has view3d!' );
		this._view3dChildren.splice( index, 1 );
		this._view3d.removeChild3D( $child3d );
	}

	public get view3dChildren():egret3d.Object3D[] {
		return this._view3dChildren;
	}

	public onResize( $e? ):void {
		this._canvas3d.x = RM.RMCanvas2DView.canvasX;
		this._canvas3d.y = RM.RMCanvas2DView.canvasY;
		var canvas = document.getElementById( "egret3D" );
		if ( canvas ) {
			canvas.style[ 'transform' ] = RM.RMCanvas2DView.canvasR;
			canvas.style['width'] = RM.RMCanvas2DView.canvasW+'px';
			canvas.style['height'] = RM.RMCanvas2DView.canvasH+'px';
		}
	}

	/**
	 * 显示区域的分辨率宽度
	 *  */
	public getClientWidth():number {
		return document.documentElement.clientWidth;
	}

	/**
	 * 显示区域的分辨率高度
	 *  */
	public getClientHeight():number {
		return document.documentElement.clientHeight;
	}

	private initHtmlCanvas():void
	{
		var canvas = document.getElementById( "egret3D" );
		if ( canvas ) {
			canvas.style[ 'position' ] = 'absolute';
			canvas.style[ 'cursor' ] = 'inherit';
			canvas.style[ 'bottom' ] = '0px';
			canvas.style[ 'right' ] = '0px';
			canvas.style[ 'transform-origin' ] = '0% 0% 0px';
		}
	}
}